<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['params']=array();
$merchid=$this->checklogin();
$return['status']=$this->order_status;
$return['buytype']=$this->buy_types;
$return['sendtype']=$this->send_types;
$return['send_status']=$this->send_status;
$return['paytype']=get_pay_types();
$return['paytype']['peerpay']='找人代付';
$return['ispay']=array('0'=>'未支付','1'=>'已支付');
$install = check_account_permission('jy_shifu');
if($install){
	model('jy_shifu/order')->order_checktime();
}
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'deleted'=>0,'plugin_name'=>'jy_mfuwu');
	$return['params']=array('deleted'=>0);
    $orderby='createtime desc';
	$total='';
	if(!empty($_RQ['ordersn'])){
		$params['ordersn']=trim($_RQ['ordersn']);
		$return['params']['ordersn']=$_RQ['ordersn'];
	}
	if(isset($_RQ['status']) && $_RQ['status'] !='100'){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}else{
		$return['params']['status']=100;
	}
	if(isset($_RQ['sendtype'])){
		$params['sendtype']=intval($_RQ['sendtype']);
		$return['params']['sendtype']=$_RQ['sendtype'];
	}
	if(isset($_RQ['buytype'])){
		$params['buytype']=intval($_RQ['buytype']);
		$return['params']['buytype']=$_RQ['buytype'];
	}
	if($_RQ['postdata']){
		$postdata=get_postdata();
		
	}
	if(trim($postdata['keyword'])){
		$params['ordersn']=trim($_RQ['ordersn']);
		$return['params']['ordersn']=trim($postdata['keyword']);
		
	}
	$return['list']=pdo_getslice('jy_weishop_order',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$v){
		$ordergoods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$v['id'],'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']),array('id','goodsid','orderid','optionid','price','total','total_price','goodssn','productsn','fee_desc'));
		$v['goods']=array();
		$v['commission_money']=0;
		$v['merch_money']=0;
		foreach($ordergoods as &$gv){
			$goodsinfo = pdo_get('jy_weishop_goods',array('id'=>$gv['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','image','title','merchid'));
			$goodsinfo['optionid']=$gv['optionid'];
			$goodsinfo['total']=$gv['total'];
			$goodsinfo['price']=$gv['price'];
			$goodsinfo['image']=tomedia($goodsinfo['image']);
			$goodsinfo['total_price']=$gv['total_price'];
			if(!$gv['fee_desc']){
					model('jy_weishop/order')->setfee_desc($gv['orderid']);
					$goodsinfo['total_price']=pdo_getcolumn('jy_weishop_order_goods',array('id'=>$gv['id']),'total_price');
			}
			$v['merch_money'] +=$goodsinfo['total_price'];
			//商品规格
			if($gv['optionid']){
				$specs=pdo_getcolumn('jy_weishop_goods_option',array('id'=>$gv['optionid']),'specs');
				$specs=explode("_",$specs);
				foreach($specs as &$spec){
					$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
				}
				$goodsinfo['selespename']=implode("，",$specs);
			}
			$v['goods'][]=$goodsinfo;
			//计算佣金
			if($this->plugin_exists('jy_commission')){
				$commission_money=model('jy_commission/commission')->get_goodsfee($gv['goodsid'],$gv['orderid'],$gv['id']);
				$v['commission_money'] +=$commission_money;
			}			
		}
		$v['merch_money']=price($v['merch_money']);
		$v['commission_money']=price($v['commission_money']);
		$v['sendtypetitle']=$return['sendtype'][$v['sendtype']];
		if($v['send_status'] && $v['status']==1){
			$v['statustitle']=$this->send_status[$v['send_status']];
		}else{
			$v['statustitle']=$this->order_status[$v['status']];
		}
		$v['buytypetitle']=$this->buy_types[$v['buytype']];
		$v['member']=cfc('core')->member_show($v['mid']);
		if($_SESSION['os']!='windows'){
			$v['createtime']=date('Y-m-d H:i:s',$v['createtime']);
		}
		$self=pdo_get('jy_weishop_order_self',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$v['id']));
		$v['selfnumber']=$self['number'];
		$v['selfnumber2']=substr($v['selfnumber'],-4);
		$v['merchorder']=model('jy_merch/commission')->get_choufee($v['id'],true,$merchid);
		$v['merchorder_commission_money']=$v['merchorder']['commission_money']?:0;
		if($v['merchorder']['status']){
			$v['merchorder_statustitle']='可结算';
		}else{
			$v['merchorder_statustitle']=$v['merchorder']['commission_money']?'不可结算':'未成交';
		}
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>array(
			array('op'=>'list','title'=>'订单管理'),
		),
		'list'=>array('data'=>'list','openvoice'=>true,'openvoice_action'=>'jy_weishop/order.list/getnew/merchid='.$merchid,'edit'=>false,'list'=>array(
			array('name'=>'ordersn','title'=>'订单号','type'=>'string'),
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'price','title'=>'金额','type'=>'string'),
			array('name'=>'merch_money','title'=>'本商户金额','type'=>'string'),
			array('name'=>'commission_money','title'=>'需扣除佣金','type'=>'string'),
			array('type'=>'img_string','title'=>'抽成情况',
			'data'=>array(
				array('title'=>'金额：￥','name'=>'merchorder_commission_money'),
				array('title'=>'状态：','name'=>'merchorder_statustitle')
				)
			),
			array('name'=>'createtime','title'=>'下单时间','type'=>'time'),
			array('name'=>'ispay','title'=>'是否支付','type'=>'radio','options'=>'ispay'),
			array('name'=>'paytype','title'=>'支付方式','type'=>'radio','options'=>'paytype'),			
		),
		'action'=>array(	
			array('type'=>'href','action'=>'/manage.order_detail/','title'=>'订单详情'),
			array('op'=>'fa','title'=>'发起服务','type'=>'modal','show'=>array('status'=>1,'sendtype'=>1,'send_status'=>array(0,11))),
			array('op'=>'fa','title'=>'门店核销','type'=>'modal','show'=>array('status'=>1,'sendtype'=>2,'send_status'=>array(0,11))),
			array('op'=>'fa','title'=>'虚拟核销','type'=>'modal','show'=>array('status'=>1,'sendtype'=>3,'send_status'=>array(0,11))),
			array('op'=>'print','title'=>'打印','type'=>'post'),
			array('type'=>'post','op'=>'shou','title'=>'确认完成','show'=>array('status'=>2)),	
		)),
		'search'=>array(
			array('name'=>'ordersn','width'=>15,'title'=>'订单号','type'=>'string'),
			array('name'=>'status','width'=>15,'title'=>'订单状态','type'=>'radio','options'=>'status'),
			array('name'=>'buytype','width'=>15,'title'=>'订单类型','type'=>'radio','options'=>'buytype'),
			array('name'=>'sendtype','width'=>15,'title'=>'服务方式','type'=>'radio','options'=>'sendtype'),
			array('name'=>'merchid','width'=>15,'title'=>'所属商户','type'=>'select','option_value'=>'id','option_title'=>'merchname','options'=>'merchs'),
		)
	);
	if($params['status']==1){
		$htmls['list']['list'][]=array('name'=>'send_status','title'=>'服务状态','type'=>'radio','options'=>'send_status');
	}else{
		$htmls['list']['list'][]=array('name'=>'status','title'=>'订单状态_status','type'=>'radio','options'=>'status');
	}
	if($_SESSION['os']=='mobile'){
		$return['htmls']=$this->template_url('manage/order_index');
	}else{
		$return['htmls']=$htmls;
	}
	exi($return);
	}
if($_RQ['op']=='export'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'plugin_name'=>'jy_mfuwu');
	$params['deleted']=intval($_RQ['deleted']);
	if(!empty($_RQ['ordersn'])){
		$params['ordersn']=trim($_RQ['ordersn']);
		$return['params']['ordersn']=$_RQ['ordersn'];
	}
	if(isset($_RQ['status']) && $_RQ['status'] !='100'){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}else{
		$return['params']['status']=100;
	}
	if(isset($_RQ['sendtype'])){
		$params['sendtype']=intval($_RQ['sendtype']);
		$return['params']['sendtype']=$_RQ['sendtype'];
	}
	if(isset($_RQ['buytype'])){
		$params['buytype']=intval($_RQ['buytype']);
		$return['params']['buytype']=$_RQ['buytype'];
	}
	$order=pdo_getall('jy_weishop_order',$params);
	$status=array('1'=>'待处理','2'=>'待服务','3'=>'已完成','4'=>'已关闭');
	$paytype=array('wechat'=>'微信支付','credit'=>'余额支付');
	$ispay=array('0'=>'未支付','1'=>'已支付');
	foreach($order as &$v){
		$v['nickname']=pdo_getcolumn('core_members',array('id'=>$v['mid']),'nickname');		
		$address=$v['address']?json_decode($v['address'],true):$v['addressid'];
		if($address){
			$v['address']=model('core/member')->get_address_str($address);
		}
		$v['mobile']=$address['mobile'];
		$v['realname']=$address['realname']?:$address['name'];
		$v['status']=$status[$v['status']];
		$v['paytype']=$paytype[$v['paytype']];
		$v['ispay']=$ispay[$v['ispay']];
		if(!$v['nickname']){
			$v['nickname']='匿名用户';
		}
		$v['createtime']=date('Y-m-d H:i:s',$v['createtime']);
		$v['storetitle']=pdo_getcolumn('jy_weishop_store',array('id'=>$v['storeid']),'title');
		$v['storetitle']=$v['storetitle']?:'无';
		$goods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$v['id']),array('goodsid','optionid'));
		foreach($goods as $k=>$gv){
			$goodstitle=pdo_getcolumn('jy_weishop_goods',array('id'=>$gv['goodsid']),'title');
			//商品规格
			if($gv['optionid']){
				$specs=pdo_getcolumn('jy_weishop_goods_option',array('id'=>$gv['optionid']),'specs');
				$specs=explode("_",$specs);
				foreach($specs as &$spec){
					$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
				}
				$selespename=implode("，",$specs);
			}else{
				$selespename='无规格';
			}
			$num=$k+1;
			$v['goodstitle'] .='【商品'.$num.'】['.$selespename.']'.$goodstitle;
		}
	}
	$header=array(
		'ordersn'=>'订单号',
		'nickname'=>'买家昵称',
		'price'=>'金额',
		'status'=>'订单状态',
		'ispay'=>'金额',
		'paytype'=>'订单状态',
		'realname'=>'联系人姓名',
		'address'=>'上门地址',
		'mobile'=>'联系人电话',
	);
	cfc('tools')->export2excel($header,$order,'订单');
}
if($_RQ['op']=='fa'){
	$install = check_account_permission('jy_shifu');
	if(!$install){
		exi('请联系管理员开通《掌上师傅》插件！','error');
	}
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$id){
			exi('参数错误！','error');
		}
		if(!$post['paisong_type']){
			exi('请选择指派任务对象！','error');
		}
		if($post['sendtype']==1){
			if(!$post['paisong_id'] && $post['need_setpeisong']){
					exi('请选择师傅！','error');
			}
			if($post['paisong_id']){
					$params['order_id']=$id;
					$params['shifu_id']=$post['paisong_id'];
					model('jy_shifu/order')->order_sendget($params);
			}else{
					$data['send_status']=12;
					$data['paisong_type']=$post['paisong_type'];
					cfc('jinyun_socket',$_SESSION['uniacid'])->send2room('刷新任务订单！','peisong_order');
					model('jy_weishop/order')->order_send($id,$data);
			}			
			exi('订单处理成功！','','/manage.order_list/list/status=1');			
		}elseif($post['sendtype']==3){
			model('jy_weishop/order')->order_send_virtual($id,$post['content']);
		}elseif($post['sendtype']==2){
			model('jy_merch/verify')->self_verify($post['verifycode']);
		}
		exi('已发货！','','/manage.order_list/list/status=1');
	}
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_weishop_order',array('id'=>$id),array('id','is_peisong','sendtype','addressid','ordersn','remark','price','isdish'));	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'订单处理',
	);
	if($item['sendtype']==1){
		$tip='服务订单已确认,请指派师傅上门处理！';
		$htmls['edit']=array(
			array('type'=>'tip','title'=>$tip),
			array('type'=>'radio','title'=>'指派任务对象','name'=>'paisong_type','options'=>'paisong_type'),
			array('type'=>'switch','name'=>'need_setpeisong','title'=>'需要指定师傅？','help'=>'不指定，则由师傅自己抢单，或系统指派！'),
			array('type'=>'select','title'=>'指定师傅','name'=>'paisong_id','options'=>'syspeisongs','option_value'=>'mid','show'=>array('paisong_type'=>1,'need_setpeisong'=>1),'help'=>'只能指定本平台的师傅（<red>在线的师傅</red>）！'),
			array('type'=>'select','title'=>'指定师傅','name'=>'paisong_id','options'=>'peisongs','option_value'=>'mid','show'=>array('paisong_type'=>2,'need_setpeisong'=>1),'help'=>'只能指定该商户下面的师傅（<red>在线的师傅</red>）！'),
		);
	}else{
		$goodsid=pdo_getcolumn('jy_weishop_order_goods',array('orderid'=>$id),'goodsid');
		$goods=pdo_get('jy_weishop_goods',array('id'=>$goodsid),array('title','image'));
		$item['verifycode']=pdo_getcolumn('jy_weishop_order_self',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$item['id']),'number');
		$item=array_merge($goods,$item);
		$htmls['edit']=array(
			array('type'=>'string','title'=>'服务名称','name'=>'title','read'=>true),
			array('type'=>'image','title'=>'项目图片','name'=>'image','read'=>true),
			array('type'=>'string','title'=>'自提核销码','name'=>'verifycode','show'=>array('sendtype'=>2)),
			array('type'=>'text','title'=>'虚拟物品核销内容','name'=>'content','show'=>array('sendtype'=>3),'help'=>'虚拟物品核销，填写核销内容！'),
		);
	}
	$peisongs=array();
	//多商户用户，选择平台区域员
	$peisongs=pdo_getall('jy_shifu_member',array('merchid'=>$merchid,'status'=>1,'online >'=>0),array('mid','title'));
	$syspeisongs=pdo_getall('jy_shifu_member',array('merchid'=>0,'status'=>1,'online >'=>0),array('mid','title'));
	$return['peisongs']=$peisongs;
	$return['syspeisongs']=$syspeisongs;
	$return['paisong_type']=array(
		1=>'指派平台师傅上门',
		2=>'指派商户自己的师傅上门',
	);
	//if($_SESSION['os']=='mobile'){
	//	$return['htmls']=$this->template_url('manage/order_index');
	//}else{
		$return['htmls']=$htmls;
	//}
	$return['item']=$item;
	exi($return);
}	

if($_RQ['op']=='ispay'){
	$id=intval($_RQ['id']);
	pdo_update('jy_weishop_order',array('ispay'=>1,'status'=>1,'pay_id'=>0),array('id'=>$id));
	pdo_update('jy_weishop_order_goods',array('clearstatus'=>3),array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$id,'clearstatus'=>0));
	exi('已确认付款','');
}

if($_RQ['op']=='shou'){
	$id=intval($_RQ['id']);
	model('jy_weishop/order')->order_receive($id);
	exi('已收货','','/manage.order_list/list/status=3');
}
if($_RQ['op']=='daifa'){
	$id=intval($_RQ['id']);
	pdo_update('jy_weishop_order',array('is_daifa'=>1),array('id'=>$id));
	exi('申请平台代发成功！','','/manage.order_list/list/status=3');
}
if($_RQ['op']=='deleted'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'deleted'=>1);
	$return['params']=array('deleted'=>1);
    $orderby='createtime desc';
	$total='';
	$return['list']=pdo_getslice('jy_weishop_order',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$v){
		$v['member']=cfc('core')->member_show($v['mid']);
	}
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'订单管理'),
		),
		'list'=>array('data'=>'list','edit'=>false,'del_op'=>'ddel','list'=>array(
			array('name'=>'ordersn','title'=>'订单号','type'=>'string'),
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'price','title'=>'金额','type'=>'string'),
			array('name'=>'status','title'=>'订单状态','type'=>'radio','options'=>'status'),	
			array('name'=>'createtime','title'=>'下单时间','type'=>'time'),
			array('name'=>'ispay','title'=>'是否支付','type'=>'radio','options'=>'ispay'),
			array('name'=>'paytype','title'=>'支付方式','type'=>'radio','options'=>'paytype'),				
		),
		'action'=>array(	
			array('type'=>'href','action'=>'manage.order_detail/','title'=>'订单详情'),
			
		),),
		
		'search'=>array(
			array('name'=>'order','width'=>15,'title'=>'订单号','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;	
	exi($return);
}
if($_RQ['op']=='print'){
	$order_id=intval($_RQ['id']);
	$res=model('jy_weishop/printmsg')->storeprint('',$order_id);
	if($res){
		exi('打印成功');
	}else{
		exi('打印失败,该订单所属门店没有添加打印任务！','error');
	}
}
if($_RQ['op']=='upprice'){
	$id=intval($_RQ['id']);
	$post=get_postdata();	
	if($post['price']){
		if(!is_numeric($post['price'])){
			exi('价格必须是数字！','error');
		}
		if(!method_exists(cfc('pay'),'change_pay_fee')){
			exi('修改价格失败！请联系管理员，更新框架到最新','error');
		}
		$price=floatval($post['price']);
		if ($price < 0) 
		{
			$price = 0;
		}
		model('jy_weishop/order')->upprice($post['id'],$price);
		exi('改价成功！','','jy_merch/manage.order_list/');	
	}
	$item=pdo_get('jy_weishop_order',array('id'=>$id),array('id','price'));
	$htmls=array(
		'type'=>'edit',
		'desc'=>'修改价格',
	);
	$htmls['edit']=array(
		array('type'=>'string','title'=>'价格','name'=>'price','help'=>'修改价格！'),	
	);
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		pdo_update('jy_weishop_order',array('deleted'=>1),array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_update('jy_weishop_order',array('deleted'=>1),array('id'=>$id));
			}
		}
	}
	exi('删除成功！','','/manage.order_list/list/');
}
if($_RQ['op']=='ddel'){

	$id=intval($_RQ['id']);
	if($id){
		$this->model('order')->order_delete($id);	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$this->model('order')->order_delete($id);
			}
		}
	}
	exi('删除成功！','','/manage.order_list/deleted');
}
?>